package org.example.qz1.controller;

import org.example.qz1.bean.Student;
import org.example.qz1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;
    @GetMapping("/Allstu")
    @CrossOrigin(origins = "http://localhost:8081")
    public List<Student> getAllStu(){
        return studentService.list();
    }
    @GetMapping("/stu/{id}")
    //@CrossOrigin(origins = "http://localhost:8081")
    public Student getStuById(@PathVariable("id") Integer id){
        Student stu = studentService.getById(id);
        return stu;
    }
    /**
     * 插入用户
     * @param stu
     * @return
     */
    @PostMapping("/stu")
    @CrossOrigin(origins = "http://localhost:8081")
    public String addStudent(@RequestBody Student stu){
        if (studentService.save(stu)){
            return "插入成功！";
        }
        else return "插入失败";
    }
    @PutMapping("/stu")
    public String updateStudent(@RequestBody Student stu){
        if (studentService.updateById(stu)){
            return "更新成功！";
        }
        else return "更新失败";
    }

    @DeleteMapping("/stu")
    public String deleteStuById(Integer id) {
        if (studentService.removeById(id)) {
            return "删除成功！";
        }
            else return "删除失败！";
    }
}
